package com.study.samsung.Dao;

import com.study.samsung.Entity.*;
import com.study.samsung.Utils.RowMapper;
import com.study.samsung.Utils.SQLHelper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodsDao {
    SQLHelper sqlHelper = new SQLHelper();

    public List<ShoppingCart> queryAllcart(Integer uid) {
        String sql = "select * from shopping_cart where uid=?";
        List<ShoppingCart> query = sqlHelper.query(sql, new ShoppingCartRowmapper(),uid);
        return query;
    }

    public Detail queryDetails(Integer sxid) {
        String sql = "select * from detail where sxid=?";
        Detail query = sqlHelper.one(sql,new DetailRowmapper(),sxid);
        return query;
    }

    public Commoditycolor querycolor(Integer yid) {
        String sql = "select * from commoditycolor where yid=?";
        return sqlHelper.one(sql, new RowMapper<Commoditycolor>() {
            @Override
            public Commoditycolor map(ResultSet rs) throws SQLException {
                return new Commoditycolor(rs.getInt("yid"),rs.getString("yname"));
            }
        },yid);
    }

    public ShoppingCart queryShoppingCart(int sxid){
        String sql = "select * from shopping_cart a left join detail d on d.sxid = a.sxid left join commoditycolor c on d.yid = c.yid\n" +
                "left join commoditymemory c2 on d.nid = c2.nid left join commodity c3 on d.sid = c3.sid where a.sxid =?";
        ShoppingCart query = sqlHelper.one(sql, new RowMapper<ShoppingCart>() {
            @Override
            public ShoppingCart map(ResultSet rs) throws SQLException {
                return new ShoppingCart(rs.getInt("gid"),
                        rs.getInt("gnum"),
                        rs.getInt("sxid"), rs.getInt("uid"),
                        rs.getBigDecimal("gprice"),
                        new Detail(rs.getInt("sxid"),
                                rs.getBigDecimal("xprice"),
                                new Commoditycolor(rs.getInt("yid"), rs.getString("yname")),
                                new Commoditymemory(rs.getInt("nid"), rs.getString("nname")),
                                new Commodity(rs.getInt("sid"), rs.getString("sname"), rs.getString("url"), rs.getBigDecimal("sprice"),rs.getBoolean("isdefault"), rs.getInt("lid"))));
            }
        }, sxid);
        return query;
    }

    public Commoditymemory querymemory(Integer nid) {
        String sql = "select * from commoditymemory where nid=?";
        return sqlHelper.one(sql, new RowMapper<Commoditymemory>() {
            @Override
            public Commoditymemory map(ResultSet rs) throws SQLException {
                return new Commoditymemory(rs.getInt("nid"),
                        rs.getString("nname"));
            }
        },nid);
    }

    public Commodity querycommodity(Integer sid) {
        String sql = "select * from commodity where sid=?";
        return sqlHelper.one(sql, new RowMapper<Commodity>() {
            @Override
            public Commodity map(ResultSet rs) throws SQLException {
                return new Commodity(rs.getInt("sid"),
                        rs.getString("sname"),
                        rs.getString("url"),
                        rs.getBigDecimal("sprice"),
                        rs.getBoolean("isdefault"),
                        rs.getInt("lid"));
            }
        },sid);
    }

    public Integer updategnum(int aftergnum, int gid) {
        String sql = "update shopping_cart set gnum=? where gid=?";
        Integer update = sqlHelper.update(sql, aftergnum, gid);
        return update;
    }

    public Integer deletecart(int gid) {
        String sql = "delete from shopping_cart where gid=?";
        Integer del = sqlHelper.update(sql, gid);
        return del;
    }

    public Integer piliangdel(int[] arrs) {
        StringBuilder stringBuilder = new StringBuilder();
        String sql = "delete from shopping_cart ";
        ArrayList<Object> params = new ArrayList<>();
        for (int i = 0; i < arrs.length; i++) {
            if (i==0){
                stringBuilder.append(" where gid=? ");
            }else {
                stringBuilder.append(" or gid=?");
            }
            params.add(arrs[i]);
        }

        Integer piliangdel = sqlHelper.update(sql + stringBuilder.toString(), params.toArray());
        return piliangdel;
    }

    public Integer querysid(int sxid) {
        String sql = "select detail.sid from detail where sxid=?";
        return sqlHelper.one(sql, new RowMapper<Integer>() {
            @Override
            public Integer map(ResultSet rs) throws SQLException {
                return rs.getInt(1);
            }
        },sxid);
    }

    public List<String> selectAllColorByCommodityId(Integer sid){
        String sql="select yname from commoditycolor a left join detail b on a.yid=b.yid where b.sid=? group by yname";
        return sqlHelper.query(sql,r-> r.getString(1),sid);
    }
    public List<String> selectAllMemoryByCommodityId(Integer sid){
        String sql="select nname from commoditymemory a left join detail b on a.nid=b.nid where b.sid=? group by nname";
        return sqlHelper.query(sql,r-> r.getString(1),sid);
    }

    public List<Object> nowcolormemory(String sxid) {
        String sql = "select yname,nname from commoditycolor a left join detail b on a.yid = b.yid left join commoditymemory c on b.nid = c.nid\n" +
                "where b.sxid=?";
        List<Object> query = sqlHelper.query(sql, new RowMapper<Object>() {
            @Override
            public Object map(ResultSet rs) throws SQLException {
                List<Object> list = new ArrayList<>();
                list.add(rs.getString(1));
                list.add(rs.getString(2));
                return list;
            }
        }, sxid);
        return query;
    }

    public Integer querynewsxid(String memoryValue, String colorValue,Integer now_sid) {
        String sql = "select b.sxid from commoditycolor a left join detail b on a.yid=b.yid left join commoditymemory c on c.nid = b.nid where a.yname=? and c.nname=? and sid=?";
        Integer sxid = sqlHelper.one(sql,rs -> {
            return rs.getInt(1);
        },colorValue,memoryValue,now_sid);
        return sxid;
    }

    public Integer updatesxid(Integer querynewsxid,Integer oldsxid) {
        String sql = "update shopping_cart set sxid=? where sxid=?";
        Integer update = sqlHelper.update(sql, querynewsxid, oldsxid);
        return update;
    }

    public Integer cartcount(Integer uid){
        String sql = "select count(gid) from shopping_cart where uid = ?";
        return sqlHelper.one(sql,rs -> {
            return rs.getInt(1);
        },uid);
    }

    class ShoppingCartRowmapper implements RowMapper<ShoppingCart>{
        @Override
        public ShoppingCart map(ResultSet rs) throws SQLException {
            return new ShoppingCart( rs.getInt("gid"),
                    rs.getInt("gnum"),
                    rs.getInt("sxid"),
                    rs.getInt("uid"),
                    rs.getBigDecimal("gprice"));
        }
    }

    class DetailRowmapper implements RowMapper<Detail>{


        @Override
        public Detail map(ResultSet rs) throws SQLException {
            return new Detail( rs.getInt("sxid"),
                    rs.getBigDecimal("xprice"),
                    rs.getInt("yid"),
                    rs.getInt("nid"),
                    rs.getInt("sid"));
        }
    }
}
